package com.imachen.ymcc.domain;

import com.baomidou.mybatisplus.enums.IdType;
import java.math.BigDecimal;
import java.util.Date;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.activerecord.Model;
import com.baomidou.mybatisplus.annotations.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * <p>
 * 
 * </p>
 *
 * @author imachen
 * @since 2022-10-20
 */
@TableName("t_kill_course")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class KillCourse extends Model<KillCourse> {


    //0即将开始，
    public static final Integer WAIT_PUBLISH = 0;
    // 1秒杀中，
    public static final Integer SUCCESS_PUBLISH = 1;
    // 2秒杀结束
    public static final Integer REMOVE_PUBLISH = 2;


    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;
    /**
     * 课程名字
     */
    @TableField("course_name")
    private String courseName;
    /**
     * 对应的课程ID
     */
    @TableField("course_id")
    private Long courseId;
    @TableField("kill_price")
    private BigDecimal killPrice;
    /**
     * 库存
     */
    @TableField("kill_count")
    private Integer killCount;
    /**
     * 每个人可以秒杀的数量,默认1
     */
    @TableField("kill_limit")
    private Integer killLimit;
    /**
     * 秒杀课程排序
     */
    @TableField("kill_sort")
    private Integer killSort;
    /**
     * 秒杀状态:0待发布，1秒杀中，2秒杀结束
     */
    @TableField("publish_status")
    private Integer publishStatus;
    @TableField("course_pic")
    private String coursePic;
    /**
     * 秒杀开始时间
     */
    @TableField("start_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss" ,timezone = "GMT+8")
    private Date startTime;
    /**
     * 秒杀结束时间
     */
    @TableField("end_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss" ,timezone = "GMT+8")
    private Date endTime;
    /**
     * 创建时间
     */
    @TableField("create_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss" ,timezone = "GMT+8")
    private Date createTime;
    /**
     * 发布到Redis的时间
     */
    @TableField("publish_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss" ,timezone = "GMT+8")
    private Date publishTime;
    /**
     * 老师，用逗号隔开
     */
    @TableField("teacher_names")
    private String teacherNames;
    /**
     * 下线时间
     */
    @TableField("offline_time")
    private Date offlineTime;
    @TableField("activity_id")
    private Long activityId;
    @TableField("time_str")
    private String timeStr;



    @Override
    protected Serializable pkVal() {
        return this.id;
    }


    public boolean getKilling(){
        Date now = new Date();
        // 当前时间在开始时间之后并且在结束时间之前
        return now.after(this.startTime) && now.before(this.endTime);
    }


    public String getKillStatusName(){
        Date now = new Date();
        if (now.before(startTime)){
            return "即将开始";
        }else if (now.after(endTime)){
            return "已结束";
        }else {
            return "秒杀中";
        }
    }

    public Long getTimeDiffMill(){
        Date now = new Date();
        if (now.before(startTime)) {
            return  (this.startTime.getTime() - now.getTime()) / 1000;
        }else if (now.after(this.startTime) && now.before(this.endTime)) {
            return  (this.endTime.getTime() - now.getTime()) / 1000;
        }
        return null;
    }


}
